package com.yuxin.tongyi.lsll.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.StreamCorruptedException;
import java.util.Calendar;

import com.yuxin.tongyi.lsll.pojo.ClassInfo;

import android.os.Environment;
import android.util.Base64;

/**
 * 文件工具类
 *
 */
public class FileTool {

	private static final int BUFFER_SIZE = 16 * 1024 ;
	
	
	/**
	 * 判断sdcard是否存在
	 * @return
	 */
	public static boolean sdCardExist() {
		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
	}

	/**
	 * 删除目录和文件
	 * 
	 * @param fi
	 * @throws IOException
	 */
	public static void deleteDirectory(File fi){
		if ((null != fi) && (fi.isDirectory() || fi.isFile())) {
			// 如果fi是文件直接删除
			if (fi.isFile()) {
				fi.delete();
			} else { // 如果是目录，获取该目录下的所有文件和文件夹
				File[] files = fi.listFiles();
				int sz = files.length;
				for (int i = 0; i < sz; i++) {
					if (files[i].isDirectory()) {
						deleteDirectory(files[i]);
					} else {
						files[i].delete();
					}
				}
			}
		} 
	}
	
	
	public static void deleteDirectoryAndParent(File fi) {
		if ((null != fi) && (fi.isDirectory() || fi.isFile())) {
			// 如果fi是文件直接删除
			if (fi.isFile()) {
				fi.delete();
			} else { // 如果是目录，获取该目录下的所有文件和文件夹
				File[] files = fi.listFiles();
				int sz = files.length;
				for (int i = 0; i < sz; i++) {
					if (files[i].isDirectory()) {
						deleteDirectoryAndParent(files[i]);
					} else {
						files[i].delete();
					}
				}
			}
			fi.delete();
		} 
	}
   
	/**
	 * 根据路径删除文件
	 * @param filename
	 * @return
	 */
	public static boolean deleteFile(String filename){
		File file = new File(filename);
		if(file.exists()){
			return file.delete();
		}
		return false;
	}
	
	/**
	 * 创建目录
	 * @param directoryName
	 * @return boolean
	 * @since Jan 6, 2010
	 */
	public static boolean createDirectory(String directoryName){
		File file=new File(directoryName);
		return file.mkdirs();
	}
	
	/**
	 * 文件复制
	 * @param src 源文件
	 * @param dst 目标文件
	 * @since Jan 21, 2010
	 */
	public static void copy(File src, File dst) {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(dst),
					BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 根据多媒体文件格式，获取图片文件类型
	 * @param mimeType 多媒体文件类型
	 * @return String
	 * @since Jan 21, 2010
	 */
	public static String getPicFileType(String mimeType){
		try {
			String typename = "";
			if (mimeType.equals("image/pjpeg")) {
				typename = ".jpg";
			} else if (mimeType.equals("image/bmp")) {
				typename = ".bmp";
			} else if (mimeType.equals("image/png")) {
				typename = ".png";
			} else if (mimeType.equals("image/gif")) {
				typename = ".gif";
			} else if (mimeType.equals("image/jpeg")) {
				typename = ".jpg";
			} else if (mimeType.equals("image/jpg")) {
				typename = ".jpg";
			} else if (mimeType.equals("image/x-png")) {
				typename = ".png";
			}
			return typename;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 修改当前系统毫秒时间
	 * @param fileType
	 * @return String
	 * @since Jan 22, 2010
	 */
	public static String getMTimeFileName(String fileType){
		try {
			Calendar cal=Calendar.getInstance();
			long mTime=cal.getTimeInMillis();
			return String.valueOf(mTime)+fileType;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取以长日期命名的文件名称
	 * @param fileName
	 * @return String
	 * @since Jan 22, 2010
	 */
	@SuppressWarnings("static-access")
	public static String getLongDataFileName(String fileType){
		try {
			Calendar calendar=Calendar.getInstance();
	    	StringBuffer fileName=new StringBuffer();
	    	fileName.append(calendar.get(calendar.YEAR));
	    	fileName.append(calendar.get(calendar.MONTH));
	    	fileName.append(calendar.get(calendar.DATE));
	    	fileName.append(calendar.get(calendar.HOUR_OF_DAY));
	    	fileName.append(calendar.get(calendar.MINUTE));
	    	fileName.append(calendar.get(calendar.SECOND));
	    	fileName.append(calendar.get(calendar.MILLISECOND));
	    	fileName.append(fileType);
	    	return fileName.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 字符串转换对象
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static Object getStringToObject(String str) throws Exception{
		Object result = null;
		if(str != null && !"".equals(str)){
			//使用commons-codec把对象字符串转换为字节数组
			byte[] bytes = org.apache.commons.codec.binary.Base64.decodeBase64(str.getBytes());
			//获取字节数组输入流
			ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
			//转为对象输入流
			ObjectInputStream is = new ObjectInputStream(stream);
			result = is.readObject();
		}
		return result;
	} 
	
	/**
	 * 对象转换字符串
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static String getObjectToString(Object obj) throws Exception{
		String result = "";
		ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
		ObjectOutputStream oStream;
		try {
			oStream = new ObjectOutputStream(baoStream);
			oStream.writeObject(obj);
		} catch (IOException e) {
			e.printStackTrace();
		}
		result = new String(org.apache.commons.codec.binary.Base64.encodeBase64(baoStream.toByteArray()));
		return result;
	} 
}
